package com.yidu.CashManagement.service.impl;

import com.google.gson.Gson;
import com.yidu.BusinessParament.entity.Cash;
import com.yidu.BusinessParament.mapper.FundMapper;
import com.yidu.CashManagement.entity.FundsAllot;
import com.yidu.CashManagement.mapper.FundsAllotMapper;
import com.yidu.CashManagement.service.FundsAllotService;
import com.yidu.CashManagement.util.CreateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 苏彬
 * @date 2022/12/10 17:02
 **/
@Service
public class FundsAllotImpl implements FundsAllotService {
    @Autowired
    private FundsAllotMapper fundsAllotMapper;

    /**
     * 查询+分页
     *
     * @param page           当前页
     * @param row            显示数
     * @param allotTime      调拨日期
     * @param allotType      调拨类型
     * @param allotDirection 调拨方向
     * @return Map
     */
    @Override
    public Map selectFundsAllot(int page, int row, String allotTime, int allotType, int allotDirection) {
        //拼接条件
        String term = append(allotTime, allotType, allotDirection).toString();
        //创建空map
        Map selectMap = new HashMap();
        //传入当前页
        selectMap.put("page", (page - 1) * row);
        //传入显示页
        selectMap.put("row", row);
        //传入条件
        selectMap.put("term", term);

        //创建layUiMap
        Map layUiMap = new HashMap();
        //传入data
        layUiMap.put("data", fundsAllotMapper.selectFundsAllot(selectMap));
        //传入msg
        layUiMap.put("msg", "");
        //传入code
        layUiMap.put("code", 0);
        //传入count
        layUiMap.put("count", fundsAllotMapper.selectFundsAllotCount(selectMap));

        //返回layUiMap
        return layUiMap;
    }

    /**
     * 删除
     * @param myArr
     */
    @Override
    public void deleteFundsAllot(String myArr) {
        //Gson转String数组
        String[] array = new Gson().fromJson(myArr, String[].class);
        //循环通过id删除
        for (String Id : array) {
            fundsAllotMapper.deleteFundsAllot(Id);
        }
    }

    /**
     * 修改
     * @param fundsAllot 修改对象
     * @return
     */
    @Override
    public String updateFundsAllot(FundsAllot fundsAllot) {
        //定义提示msg
        String msg = "";
        try {
            //执行资金调拨修改
            int i = fundsAllotMapper.updateFundsAllot(fundsAllot);
            //i>=1 则ok  报错则error
            if(i>=1){
                msg="ok";
            }
        } catch (Exception e) {
            msg = "error";
        }
        //返回msg
        return msg;
    }

    /**
     * 查询现金账户
     * @return
     */
    @Override
    public Map SelectCash() {
        //创建map
        Map map=new HashMap();
        //传入msg
        map.put("msg","");
        //传入count
        map.put("count",fundsAllotMapper.selectCashCount());
        //传入code
        map.put("code",0);
        //传入data
        map.put("data",fundsAllotMapper.selectCash());
        //返回map
        return map;
    }

    @Override
    public int deleteFundsAllotBusinessId(String businessId) {
        return fundsAllotMapper.deleteFundsAllotBusinessId(businessId);
    }

    /**
     * 增加
     * @param fundsAllot 增加对象
     * @return
     */
    @Override
    public int addFundsAllot(FundsAllot fundsAllot) {
        //创建id
        String id= CreateId.createId("FA");
        //赋值资金调拨id
        fundsAllot.setFundsAllotId(id);
        //返回执行行数
        return fundsAllotMapper.addFundsAllot(fundsAllot);
    }

    /**
     * 拼接条件
     * @param allotTime 时间
     * @param allotType 类型
     * @param allotDirection 流入流出
     * @return
     */
    public StringBuffer append(String allotTime, int allotType, int allotDirection) {
        //创建空StringBuffer对象
        StringBuffer term = new StringBuffer();
        //判断allotTime是否为空 拼接
        if (allotTime != null && !allotTime.equals("")) {
            term.append(" and allotTime='").append(allotTime).append("'");
        }
        //判断allotType是否>0 拼接
        if (allotType > 0) {
            term.append(" and allotType=").append(allotType);
        }
        //判断allotDirection是否为!=-2 拼接
        if (allotDirection !=-2) {
            term.append(" and allotDirection=").append(allotDirection);
        }
        //返回term
        return term;
    }
}
